package com.sskj.hhrt.transaction;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.utils.DateUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.hhrt.MVPFragment;
import com.sskj.hhrt.R;
import com.sskj.hhrt.transaction.data.OrderHistoryBean;

import java.util.List;

import butterknife.BindView;

/**
 * 成交记录
 */
public class DealFragment extends MVPFragment<DealPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;

    private BaseAdapter<OrderHistoryBean.OrderHistory> baseAdapter;
    private int page = 1;
    private int pageSize = 10;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_deal;
    }

    @Override
    public void initView() {
        initRefreshLayout(smartRefreshLayout);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getContext())
                .setFirstDraw(false)
                .setPaintWidth(20)
                .setDividerColor(Color.parseColor("#00ffffff"))
        );
        baseAdapter = new BaseAdapter<OrderHistoryBean.OrderHistory>(R.layout.item_deal, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, OrderHistoryBean.OrderHistory item) {
                holder.setText(R.id.tv_type, item.getOtype() == 1 ? "融资" : "融券")
                        .setTextColor(R.id.tv_type, getResources().getColor(item.getOtype() == 1 ? R.color.text_e83 : R.color.color_03c087))
                        .setText(R.id.tv_name, item.getPname())
                        .setText(R.id.tv_buy_price, item.getBuyprice())  // 买价
                        .setText(R.id.tv_sell_price, item.getSellprice())  // 卖价
                        .setText(R.id.tv_amount, item.getBuynum() + "")// 手数
                        .setText(R.id.tv_profit_loss, item.getProfit())// 盈亏
                        .setText(R.id.tv_service_charge, item.getSxfee())// 手续费
                        .setText(R.id.tv_promise_money, item.getTotalprice())  // 保证金
                        .setText(R.id.tv_stop_profit, item.getStopProfitPrice())
                        .setText(R.id.tv_stop_loss, item.getStopLossPrice())
                        .setText(R.id.tv_night_price, item.getDayfee())  // 过夜费
                        .setText(R.id.tv_create_time, DateUtil.getDateFormat(Long.valueOf(Long.valueOf(item.getSelltime()) * 1000), DateUtil.PATTERN_J));

                if (Float.parseFloat(item.getProfit()) > 0) {
                    holder.setTextColor(R.id.tv_profit_loss, color(R.color.red));
                } else {
                    holder.setTextColor(R.id.tv_profit_loss, color(R.color.green));
                }
                if (item.getOtype() == 1) {
                    holder.setTextColor(R.id.tv_type, color(R.color.red));
                } else {
                    holder.setTextColor(R.id.tv_type, color(R.color.green));
                }
            }
        };
    }

    @Override
    public void initData() {
        mPresenter.getTodayEntrust(account, page, pageSize);
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getTodayEntrust(account, page, pageSize);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getTodayEntrust(account, page, pageSize);
            }
        });
    }

    @Override
    public DealPresenter getPresenter() {
        return new DealPresenter();
    }

    public void setData(List<OrderHistoryBean.OrderHistory> data) {
        if (page == 1) {
            baseAdapter.setNewData(data);
        } else {
            if (baseAdapter.getData().size() != 0) {
                baseAdapter.addData(data);
            }
        }
    }

    public void finishRefresh() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }

    public static DealFragment newInstance() {
        Bundle args = new Bundle();
        DealFragment fragment = new DealFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
